package cn.enn.smart.service.impl;

import cn.enn.smart.model.EnnUserDetails;
import cn.enn.smart.service.UserDetailsServiceExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;

public class EnnUserDetailsServiceImpl implements UserDetailsServiceExtension {

	private static final Logger LOGGER = LoggerFactory.getLogger(EnnUserDetailsServiceImpl.class);

	public EnnUserDetailsServiceImpl(PasswordEncoder passwordEncoder) {
		this.passwordEncoder = passwordEncoder;
	}

	private final PasswordEncoder passwordEncoder;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		// TODO load user from db
		String password = passwordEncoder.encode("123456");
		LOGGER.info("使用默认的UserDetailsService,用户名任意填写，密码[123456]");
		return new EnnUserDetails(username, password, AuthorityUtils.createAuthorityList("admin"));
	}

	@Override
	public UserDetails loadUserByMobile(String mobile) throws UsernameNotFoundException {
		// TODO load user from db
		String password = passwordEncoder.encode("123456");
		LOGGER.info("使用默认的UserDetailsService,手机号任意填写，密码或验证码为[123456]");
		return new EnnUserDetails(mobile, password, AuthorityUtils.createAuthorityList("admin"));
	}
}
